<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="借款人"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="companyId" v-if="!showCompany">
        <el-select
          v-model="queryParams.companyId"
          placeholder="平台"
          clearable
          style="width: 240px"
          @change="companyChange"
        >
          <el-option
            v-for="item in companyList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="" prop="collectionBy">
        <el-select
          v-model="queryParams.collectionBy"
          placeholder="催收人"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in overUserList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="balancerBy">
        <el-select
          v-model="queryParams.balancerBy"
          placeholder="平账人"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in balancerList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="还款开始日期"
          end-placeholder="还款结束日期"
        ></el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="平台" align="center" prop="companyId"/>
      <el-table-column label="订单号" align="center" prop="orderSn"/>
      <el-table-column label="贷款用户" align="center" prop="userName"/>
      <el-table-column label="手机号码" align="center" prop="userPhone"/>
      <el-table-column label="贷款金额" align="center" prop="realApplyMoney"/>
      <el-table-column label="逾期金额" align="center" prop="overMoney"/>
      <el-table-column label="已还金额" align="center" prop="alreadyRepayMoney"/>
      <el-table-column label="未还金额" align="center" prop="alreadyRepayMoney">
        <template slot-scope="scope">
         {{scope.row.realApplyMoney + scope.row.overMoney - scope.row.alreadyRepayMoney}}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '7'">已还款</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打款时间" align="center" prop="applyTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应还时间" align="center" prop="repaymentTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.repaymentTime.substring(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最终还款时间" align="center" prop="alreadyRepayTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.alreadyRepayTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >
            <router-link :to="'/app/user-info/index/' + scope.row.id + '/' + scope.row.userId" class="link-type">
              <span>查看</span>
            </router-link>
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >
            <router-link :to="'/app/apply-payment-log/index/' + scope.row.userId" class="link-type">
              <span>还款记录</span>
            </router-link>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listRepaySuccessApply, examineApply, listExamineUser} from "@/api/app/apply";
import {list} from "@/api/company"
import {listOverUser,balancerList} from "@/api/system/user";

export default {
  name: "Payment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 是否显示平台查询
      showCompany: this.$store.state.user.companyId,
      // 总条数
      total: 0,
      // 订单表格数据
      applyList: null,
      // 催收人
      overUserList: null,
      // 平账人
      balancerList: null,
      //平台
      companyList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        userPhone: undefined,
        orderSn: undefined,
        companyId: undefined,
        collectionBy: undefined,
        balancerBy: undefined,
        status: '7',
      },
      // 表单校验
      rules: {
        applyMoney: [
          {required: true, message: "贷款不能为空", trigger: "blur"},
        ]
      }
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getOverUserList();
    this.getBalancerList();
    if(!this.showCompany){
      this.getCompanyList();
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listRepaySuccessApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    /** 查询催收人 */
    getOverUserList(companyId) {
      listOverUser({companyId:companyId}).then(response => {
          this.overUserList = response.data;
        }
      );
    },

    /** 查询平账人 */
    getBalancerList(companyId) {
      balancerList({companyId:companyId}).then(response => {
          this.balancerList = response.data;
        }
      );
    },

    /** 查询平台 */
    getCompanyList() {
      list().then(response => {
          this.companyList = response.data;
        }
      );
    },

    companyChange(){
      this.queryParams.collectionBy = undefined;
      this.getOverUserList(this.queryParams.companyId);
      this.queryParams.balancerBy = undefined;
      this.getBalancerList(this.queryParams.companyId);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        applyMoney: 0,
        manageMoney: 0,
        examineMoney: 0,
        serviceMoney: 0,
        cycle: 0,
        realMoney: 0,
      };
      this.resetForm("form");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
  }
};
</script>
